﻿using Crm.CrmClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public partial class Survey : ChildWindow
    {
        CrmClient.CrmServiceClient srv = new CrmClient.CrmServiceClient();
        Soru soru = null;
        public int MusteriID { get; set; }
        int AnketID;

        public Survey()
        {
            InitializeComponent();
            this.Loaded += Survey_Loaded;
            srv.SoruGetirCompleted += srv_SoruGetirCompleted;
            srv.AnketlerCompleted += srv_AnketlerCompleted;
        }

        void srv_AnketlerCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            counter--;
            if (counter == 0)
            {
                srv.SoruGetirAsync(MusteriID);
            }
        }

        void srv_SoruGetirCompleted(object sender, CrmClient.SoruGetirCompletedEventArgs e)
        {
            soru = null;
            soru = e.Result as Soru;
            if (soru != null)
            {
                lblSoru.Text = soru.Tanim;
                stackSoru.Children.Clear();
                if (soru.CokluSecim)
                {
                    CheckBox chk = null;
                    for (int i = 0; i < soru.Secenek.Count; i++)
                    {
                        chk = new CheckBox();
                        chk.Content = soru.Secenek[i];
                        chk.Margin = new Thickness { Top = 10 };
                        chk.FontSize = 14;
                        chk.Tag = soru.CevapID[i];
                        stackSoru.Children.Add(chk);
                    }
                }
                else
                {
                    RadioButton chk = null;
                    for (int i = 0; i < soru.Secenek.Count; i++)
                    {
                        chk = new RadioButton();
                        chk.Content = soru.Secenek[i];
                        chk.Margin = new Thickness { Top = 10, Left = 10 };
                        chk.FontSize = 14;
                        chk.Tag = soru.CevapID[i];
                        stackSoru.Children.Add(chk);
                    }
                }
                if (soru.OzelSecenek)
                {
                    TextBox txt = new TextBox();
                    Label lbl = new Label();
                    lbl.Margin = new Thickness { Top = 10, Left = 10 };
                    txt.Margin = new Thickness { Top = 10, Left = 10 };
                    lbl.Content = "Diğer";
                    lbl.FontSize = 14;
                    stackSoru.Children.Add(lbl);
                    stackSoru.Children.Add(txt);
                }
            }
            else
            {
                DialogResult = true;
            }
        }

        void Survey_Loaded(object sender, RoutedEventArgs e)
        {
            srv.SoruGetirAsync(MusteriID);
        }

        int counter = 0;
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            string ozelCevap = "";
            foreach (var item in stackSoru.Children)
            {
                if (item.GetType() == typeof(TextBox))
                {
                    ozelCevap = ((TextBox)item).Text;
                }
            }

            foreach (var item in stackSoru.Children)
            {
                if (soru.CokluSecim)
                {
                    if (item.GetType() == typeof(CheckBox))
                        if ((bool)((CheckBox)item).IsChecked)
                            counter++;
                }
                else
                {
                    if (item.GetType() == typeof(RadioButton))
                        if ((bool)((RadioButton)item).IsChecked)
                            counter++;
                }
            }

            foreach (var item in stackSoru.Children)
            {
                if (soru.CokluSecim)
                {
                    if (item.GetType() == typeof(CheckBox))
                        if ((bool)((CheckBox)item).IsChecked)
                            srv.AnketlerAsync(-1, 0, MusteriID, AnketID, soru.ID, Convert.ToInt32(((CheckBox)item).Tag), ozelCevap, DateTime.Now);
                }
                else
                {
                    if (item.GetType() == typeof(RadioButton))
                        if ((bool)((RadioButton)item).IsChecked)
                            srv.AnketlerAsync(-1, 0, MusteriID, AnketID, soru.ID, Convert.ToInt32(((RadioButton)item).Tag), ozelCevap, DateTime.Now);
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

